<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/groups.qdoc -->
<head>
  <title>Qt 4.3: Implicitly Shared Classes</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Implicitly Shared Classes<br /><small></small></h1>
<a name="implicit-data-sharing"></a><a name="implicit-sharing"></a><a name="implicitly-shared"></a><a name="reference-counting"></a><a name="shared-implicitly"></a><a name="shared-classes"></a><p>Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying of data. The only important effect is that the classes listed here <i>can</i> be passed around as arguments efficiently, even though they may seem heavyweight.</p>
<ul><li><a href="#overview">Overview</a></li>
<li><a href="#implicit-sharing-in-detail">Implicit Sharing in Detail</a></li>
<li><a href="#list-of-classes">List of Classes</a></li>
</ul>
<a name="overview"></a>
<h3>Overview</h3>
<p>A shared class consists of a pointer to a shared data block that contains a reference count and the data.</p>
<p>When a shared object is created, it sets the reference count to 1. The reference count is incremented whenever a new object references the shared data, and decremented when the object dereferences the shared data. The shared data is deleted when the reference count becomes zero.</p>
<a name="deep-copy"></a><a name="shallow-copy"></a><p>When dealing with shared objects, there are two ways of copying an object. We usually speak about <i>deep</i> and <i>shallow</i> copies. A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e&#x2e; just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count.</p>
<p>Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.</p>
<p>The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.</p>
<p>Implicit sharing takes place behind the scenes; the programmer does not need to worry about it. Even in multithreaded applications, implicit sharing takes place, as explained in <a href="threads.html#threads-and-implicit-sharing">Threads and Implicit Sharing</a>.</p>
<a name="implicit-sharing-in-detail"></a>
<h3>Implicit Sharing in Detail</h3>
<p>Implicit sharing automatically detaches the object from a shared block if the object is about to change and the reference count is greater than one. (This is often called &quot;copy-on-write&quot; or &quot;value semantics&quot;.)</p>
<p>An implicitly shared class has total control of its internal data. In any member functions that modify its data, it automatically detaches before modifying the data.</p>
<p>The <a href="qpen.html">QPen</a> class, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.</p>
<p>Code fragment:</p>
<pre> void QPen::setStyle(Qt::PenStyle style)
 {
     detach();           <span class="comment">//</span> detach from common data
     d-&gt;style = style;   <span class="comment">//</span> set the style member
 }

 void QPen::detach()
 {
     if (d-&gt;ref != 1) {
         ...             <span class="comment">//</span> perform a deep copy
     }
 }</pre>
<a name="list-of-classes"></a>
<h3>List of Classes</h3>
<p>The classes listed below automatically detach from common data if an object is about to be changed. The programmer will not even notice that the objects are shared. Thus you should treat separate instances of them as separate objects. They will always behave as separate objects but with the added benefit of sharing data whenever possible. For this reason, you can pass instances of these classes as arguments to functions by value without concern for the copying overhead.</p>
<p>Example:</p>
<pre> QPixmap p1, p2;
 p1.load(&quot;image.bmp&quot;);
 p2 = p1;                        <span class="comment">//</span> p1 and p2 share data

 QPainter paint;
 paint.begin(&amp;p2);               <span class="comment">//</span> cuts p2 loose from p1
 paint.drawText(0,50, &quot;Hi&quot;);
 paint.end();</pre>
<p>In this example, <tt>p1</tt> and <tt>p2</tt> share data until <a href="qpainter.html#begin">QPainter::begin</a>() is called for <tt>p2</tt>, because painting a pixmap will modify it.</p>
<p><b>Warning:</b> Do not copy an implicitly shared container (<a href="qmap.html">QMap</a>, <a href="qvector.html">QVector</a>, etc.) while you are iterating over it using an non-const <a href="containers.html#stl-style-iterators">STL-style iterator</a>.</p>
<p><table width="100%" class="annotated" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><th><a href="qbitarray.html">QBitArray</a></th><td>Array of bits</td></tr>
<tr valign="top" class="even"><th><a href="qbitmap.html">QBitmap</a></th><td>Monochrome (1-bit depth) pixmaps</td></tr>
<tr valign="top" class="odd"><th><a href="qbrush.html">QBrush</a></th><td>Defines the fill pattern of shapes drawn by QPainter</td></tr>
<tr valign="top" class="even"><th><a href="qbytearray.html">QByteArray</a></th><td>Array of bytes</td></tr>
<tr valign="top" class="odd"><th><a href="qcache.html">QCache</a></th><td>Template class that provides a cache</td></tr>
<tr valign="top" class="even"><th><a href="qcursor.html">QCursor</a></th><td>Mouse cursor with an arbitrary shape</td></tr>
<tr valign="top" class="odd"><th><a href="qdir.html">QDir</a></th><td>Access to directory structures and their contents</td></tr>
<tr valign="top" class="even"><th><a href="qfileinfo.html">QFileInfo</a></th><td>System-independent file information</td></tr>
<tr valign="top" class="odd"><th><a href="qfont.html">QFont</a></th><td>Font used for drawing text</td></tr>
<tr valign="top" class="even"><th><a href="qfontinfo.html">QFontInfo</a></th><td>General information about fonts</td></tr>
<tr valign="top" class="odd"><th><a href="qfontmetrics.html">QFontMetrics</a></th><td>Font metrics information</td></tr>
<tr valign="top" class="even"><th><a href="qfontmetricsf.html">QFontMetricsF</a></th><td>Font metrics information</td></tr>
<tr valign="top" class="odd"><th><a href="qglcolormap.html">QGLColormap</a></th><td>Used for installing custom colormaps into QGLWidgets</td></tr>
<tr valign="top" class="even"><th><a href="qgradient.html">QGradient</a></th><td>Used in combination with QBrush to specify gradient fills</td></tr>
<tr valign="top" class="odd"><th><a href="qhash.html">QHash</a></th><td>Template class that provides a hash-table-based dictionary</td></tr>
<tr valign="top" class="even"><th><a href="qicon.html">QIcon</a></th><td>Scalable icons in different modes and states</td></tr>
<tr valign="top" class="odd"><th><a href="qimage.html">QImage</a></th><td>Hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device</td></tr>
<tr valign="top" class="even"><th><a href="qkeysequence.html">QKeySequence</a></th><td>Encapsulates a key sequence as used by shortcuts</td></tr>
<tr valign="top" class="odd"><th><a href="qlinkedlist.html">QLinkedList</a></th><td>Template class that provides linked lists</td></tr>
<tr valign="top" class="even"><th><a href="qlist.html">QList</a></th><td>Template class that provides lists</td></tr>
<tr valign="top" class="odd"><th><a href="qlocale.html">QLocale</a></th><td>Converts between numbers and their string representations in various languages</td></tr>
<tr valign="top" class="even"><th><a href="qmap.html">QMap</a></th><td>Template class that provides a skip-list-based dictionary</td></tr>
<tr valign="top" class="odd"><th><a href="qmultihash.html">QMultiHash</a></th><td>Convenience QHash subclass that provides multi-valued hashes</td></tr>
<tr valign="top" class="even"><th><a href="qmultimap.html">QMultiMap</a></th><td>Convenience QMap subclass that provides multi-valued maps</td></tr>
<tr valign="top" class="odd"><th><a href="qpalette.html">QPalette</a></th><td>Color groups for each widget state</td></tr>
<tr valign="top" class="even"><th><a href="qpen.html">QPen</a></th><td>Defines how a QPainter should draw lines and outlines of shapes</td></tr>
<tr valign="top" class="odd"><th><a href="qpicture.html">QPicture</a></th><td>Paint device that records and replays QPainter commands</td></tr>
<tr valign="top" class="even"><th><a href="qpixmap.html">QPixmap</a></th><td>Off-screen image representation that can be used as a paint device</td></tr>
<tr valign="top" class="odd"><th><a href="qpolygon.html">QPolygon</a></th><td>Vector of points using integer precision</td></tr>
<tr valign="top" class="even"><th><a href="qpolygonf.html">QPolygonF</a></th><td>Vector of points using floating point precision</td></tr>
<tr valign="top" class="odd"><th><a href="qqueue.html">QQueue</a></th><td>Generic container that provides a queue</td></tr>
<tr valign="top" class="even"><th><a href="qregexp.html">QRegExp</a></th><td>Pattern matching using regular expressions</td></tr>
<tr valign="top" class="odd"><th><a href="qregion.html">QRegion</a></th><td>Clip region for a painter</td></tr>
<tr valign="top" class="even"><th><a href="qset.html">QSet</a></th><td>Template class that provides a hash-table-based set</td></tr>
<tr valign="top" class="odd"><th><a href="qsqlfield.html">QSqlField</a></th><td>Manipulates the fields in SQL database tables and views</td></tr>
<tr valign="top" class="even"><th><a href="qsqlquery.html">QSqlQuery</a></th><td>Means of executing and manipulating SQL statements</td></tr>
<tr valign="top" class="odd"><th><a href="qsqlrecord.html">QSqlRecord</a></th><td>Encapsulates a database record</td></tr>
<tr valign="top" class="even"><th><a href="qstack.html">QStack</a></th><td>Template class that provides a stack</td></tr>
<tr valign="top" class="odd"><th><a href="qstring.html">QString</a></th><td>Unicode character string</td></tr>
<tr valign="top" class="even"><th><a href="qstringlist.html">QStringList</a></th><td>List of strings</td></tr>
<tr valign="top" class="odd"><th><a href="qtextcursor.html">QTextCursor</a></th><td>Offers an API to access and modify QTextDocuments</td></tr>
<tr valign="top" class="even"><th><a href="qtextdocumentfragment.html">QTextDocumentFragment</a></th><td>Represents a piece of formatted text from a QTextDocument</td></tr>
<tr valign="top" class="odd"><th><a href="qtextformat.html">QTextFormat</a></th><td>Formatting information for a QTextDocument</td></tr>
<tr valign="top" class="even"><th><a href="qurl.html">QUrl</a></th><td>Convenient interface for working with URLs</td></tr>
<tr valign="top" class="odd"><th><a href="qvariant.html">QVariant</a></th><td>Acts like a union for the most common Qt data types</td></tr>
<tr valign="top" class="even"><th><a href="qvector.html">QVector</a></th><td>Template class that provides a dynamic array</td></tr>
<tr valign="top" class="odd"><th><a href="qx11info.html">QX11Info</a></th><td>Information about the X display configuration</td></tr>
</table></p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
